package com.wjc.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjc.wiki.entity.Wikiuser;
import com.wjc.wiki.entity.WikiuserExample;
import com.wjc.wiki.exception.BusinessException;
import com.wjc.wiki.exception.BusinessExceptionCode;
import com.wjc.wiki.mapper.WikiuserMapper;
import com.wjc.wiki.req.WikiuserLoginReq;
import com.wjc.wiki.req.WikiuserQueryReq;
import com.wjc.wiki.req.WikiuserResetPasswordReq;
import com.wjc.wiki.req.WikiuserSaveReq;
import com.wjc.wiki.resp.WikiuserLoginResp;
import com.wjc.wiki.resp.WikiuserQueryResp;
import com.wjc.wiki.resp.PageResp;
import com.wjc.wiki.util.CopyUtil;
import com.wjc.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WikiuserService {

    private static final Logger LOG= LoggerFactory.getLogger(WikiuserService.class);

    @Autowired
    private WikiuserMapper wikiuserMapper;

    @Resource
    private SnowFlake snowFlake;

    public PageResp<WikiuserQueryResp> list(WikiuserQueryReq req){

        WikiuserExample wikiuserExample =new WikiuserExample();
        WikiuserExample.Criteria criteria=wikiuserExample.createCriteria();

        if (!ObjectUtils.isEmpty(req.getLoginName())){//动态sql,如果req的name不为空
            criteria.andLoginNameEqualTo(req.getLoginName());//使用where条件查询
        }

        //将查询到的对象转换为wikiuserresp,复制wikiuser对象到wikiuserresp
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Wikiuser> wikiuserList = wikiuserMapper.selectByExample(wikiuserExample);

        PageInfo<Wikiuser> pageInfo=new PageInfo<>(wikiuserList);
        LOG.info("总行数:{}",pageInfo.getTotal());
        LOG.info("总页数:{}",pageInfo.getPages());

//        List<WikiuserResp> respList =new ArrayList<>();
//        for (Wikiuser wikiuser : wikiuserList) {
////           WikiuserResp wikiuserResp = new WikiuserResp();
////           BeanUtils.copyProperties(wikiuser,wikiuserResp);//beanutil springboot自带,方便复制对象
//           //对象复制
//            WikiuserResp wikiuserResp = CopyUtil.copy(wikiuser,WikiuserResp.class);
//
//           respList.add(wikiuserResp);//将转换完成的对象添加到list中
//        }
        //列表复制
        List<WikiuserQueryResp> wikiuserRespList =CopyUtil.copyList(wikiuserList, WikiuserQueryResp.class);

        PageResp<WikiuserQueryResp> pageResq=new PageResp();
        pageResq.setTotal(pageInfo.getTotal());
        pageResq.setList(wikiuserRespList);

        return pageResq;
    }

    //保存
    public void save(WikiuserSaveReq req){
        Wikiuser wikiuser =CopyUtil.copy(req,Wikiuser.class);
        if (!ObjectUtils.isEmpty(req.getId())){//动态sql,如果req的id不为空,更新 否则新增
            //新增
            wikiuser.setLoginName(null);
            wikiuser.setPassword(null);
            wikiuserMapper.updateByPrimaryKeySelective(wikiuser);//动态校验，只有传入有值才会去更新
        }else{
            Wikiuser userDB=selectByLoginName(req.getLoginName());
            if(ObjectUtils.isEmpty(userDB)){//如果为空
                wikiuser.setId(snowFlake.nextId());
                wikiuserMapper.insert(wikiuser);
            }else{
                //用户名已存在
                //抛出自定义异常
                throw  new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);

            }

        }

    }

    public void delete(Long id){
        wikiuserMapper.deleteByPrimaryKey(id);
    }

    public Wikiuser selectByLoginName(String loginname){
        WikiuserExample wikiuserExample =new WikiuserExample();
        WikiuserExample.Criteria criteria=wikiuserExample.createCriteria();
        criteria.andLoginNameEqualTo(loginname);//使用where条件查询
        List<Wikiuser> wikiuserList =wikiuserMapper.selectByExample(wikiuserExample);
        if(CollectionUtils.isEmpty(wikiuserList)){
            return null;
        }else{
          return  wikiuserList.get(0);
        }

    }



    //修改密码
    public void resetPasswird(WikiuserResetPasswordReq req){
        Wikiuser wikiuser =CopyUtil.copy(req,Wikiuser.class);
        wikiuserMapper.updateByPrimaryKeySelective(wikiuser);//动态校验，只有传入有值才会去更新
    }

    //登录
    public WikiuserLoginResp login(WikiuserLoginReq req){
        Wikiuser wikiuserDb=selectByLoginName(req.getLoginName());
        if(ObjectUtils.isEmpty(wikiuserDb)){
            //用户名不存在
            LOG.info("用户名不存在,{}",req.getLoginName());
            throw  new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }else{
            //对比密码
            if(wikiuserDb.getPassword().equals(req.getPassword())){
                //登录成功
                WikiuserLoginResp wikiuserLoginResp=CopyUtil.copy(wikiuserDb,WikiuserLoginResp.class);
                return  wikiuserLoginResp;
            }else{
                //密码错误
                LOG.info("密码错误,输入密码:{},数据库密码:{}",req.getPassword(),wikiuserDb.getPassword());
                throw  new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }
        }
    }
}
